#include "ximea.h"
#include <QImage>
#include <QDateTime>

XI_IMG image;

Ximea::Ximea(int exp, double gai, QObject *parent) : QThread(parent)
{
    ximea_stat = false;
    ximea_number = 0;

    memset(&image,0,sizeof(image));
    image.size = sizeof(XI_IMG);

    xiH = NULL;
    stat = XI_OK;
    exposure = exp;
    gain = gai;
    for(int i=0;i<256;i++)
    {
        qVector.push_back(qRgb(i,i,i));
    }
    sa = new Save;
    connect(sa,SIGNAL(saveokSignal()), this, SLOT(ximeaokSlot()));
}

Ximea::~Ximea()
{

    xiStopAcquisition(xiH);
    xiCloseDevice(xiH);
    delete sa;
}

void Ximea::timerOutSlot()
{
    XI_RETURN _stat;
    ximea_number = 0;
    _stat = xiGetNumberDevices(&ximea_number);

    if(ximea_number < 1)
    {
        ximea_stat = false;
    }
}

void Ximea::exposureSlot(int exp)
{
    exposure = exp;
    stat = xiSetParamInt(xiH, XI_PRM_EXPOSURE, exposure);
    HandleResult(stat,"xiSetParam (exposure set)");
}
void Ximea::gainSlot(double gai)
{
    gain = gai;
    stat = xiSetParamFloat(xiH, XI_PRM_GAIN, gain);
    HandleResult(stat,"xiSetParam (gian set)");
}




void Ximea::shotSlot()
{
    exposure = V30D.getExpo();      //获取应该设置的曝光时间
    if(exposure > 10000) {       //当曝光时间超过4000us时，设置为4000us
        exposure = 10000;
    }

    stat = xiSetParamInt(xiH, XI_PRM_EXPOSURE, exposure);
    HandleResult(stat,"xiSetParam (exposure set)");
    stat = xiGetImage(xiH, 5000, &image);
    HandleResult(stat,"xiGetImage");

    sa->start();
}

void Ximea::ximeaokSlot()
{
    emit ximeaokSignal();
}

void Ximea::run()
{
    char buf[20] = {0};
    DWORD len;
    XI_PRM_TYPE type = xiTypeString;
    while(1)
    {
        timerOutSlot();
        if(ximea_number > 0)
        {
            if(ximea_stat == false)
            {
                xiStopAcquisition(xiH);
                xiCloseDevice(xiH);


                stat = xiGetDeviceInfo(0,XI_PRM_DEVICE_NAME,buf,&len,&type);
                HandleResult(stat,"xiGetDeviceInfo");
                emit deviceTypeSignal(QString(buf));

                qDebug()<<"Opening camera...";
                stat = xiOpenDevice(0, &xiH);
                HandleResult(stat,"xiOpenDevice");

                exposure = V30D.getExpo();      //获取应该设置的曝光时间
                if(exposure > 10000) {       //当曝光时间超过4000us时，设置为4000us
                    exposure = 10000;
                }

                stat = xiSetParamInt(xiH, XI_PRM_EXPOSURE, exposure);
                HandleResult(stat,"xiSetParam (exposure set)");
                stat = xiSetParamFloat(xiH, XI_PRM_GAIN, gain);
                HandleResult(stat,"xiSetParam (gian set)");

                xiSetParamInt(xiH,XI_PRM_IMAGE_DATA_FORMAT,XI_RAW16);

                qDebug()<<"Starting acquisition...";
                stat = xiStartAcquisition(xiH);
                HandleResult(stat,"xiStartAcquisition");

                ximea_stat = true;
                emit ximeastartSignal();


                usleep(1000*500);

            }
            else
            {
#if VIDEO == 1
                stat = xiGetImage(xiH, 5000, &image);
                HandleResult(stat,"xiGetImage");

                emit readImageSignal();
#endif

            }
        }
        usleep(1000*100);
    }
}
